home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / shells / bashsrc.zoo / print_cmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-05  |  12.3 KB  |  531 lines

  1. /* print_command -- A way to make readable commands from a command tree. */
  2.  
  3. /* Copyright (C) 1989 Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Bash, the Bourne Again SHell.
  6.  
  7. Bash is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 1, or (at your option) any later
  10. version.
  11.  
  12. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License along
  18. with Bash; see the file COPYING.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include <stdio.h>
  22.  
  23. #if defined (HAVE_VPRINTF)
  24. #include <varargs.h>
  25. #endif
  26.  
  27. #include "shell.h"
  28. #include "y.tab.h"
  29.  
  30. static int indentation = 0;
  31. static int indentation_amount = 4;
  32.  
  33. #define PRINTED_COMMAND_GROW_SIZE 1024
  34. char *the_printed_command = (char *)NULL;
  35. int the_printed_command_size = 0;
  36. int command_string_index = 0;
  37.  
  38. /* Non-zero means the stuff being printed is inside of a function def. */
  39. static int inside_function_def = 0;
  40. static int skip_this_indent = 0;
  41.  
  42. /* Print COMMAND (a command tree) on standard output. */
  43. print_command (command)
  44.      COMMAND *command;
  45. {
  46.   char *make_command_string ();
  47.   command_string_index = 0;
  48.   printf ("%s", make_command_string (command));
  49. }
  50.  
  51. /* Make a string which is the printed representation of the command
  52.    tree in COMMAND.  We return this string.  However, the string is
  53.    not consed, so you have to do that yourself if you want it to
  54.    remain around. */
  55. char *
  56. make_command_string (command)
  57.      COMMAND *command;
  58. {
  59.   command_string_index = 0;
  60.   make_command_string_internal (command);
  61.   return (the_printed_command);
  62. }
  63.  
  64. /* The internal function.  This is the real workhorse. */
  65. make_command_string_internal (command)
  66.      COMMAND *command;
  67. {
  68.   if (!command)
  69.     {
  70.       cprintf ("");
  71.     }
  72.   else
  73.     {
  74.       if (skip_this_indent)
  75.     skip_this_indent--;
  76.       else
  77.     indent (indentation);
  78.  
  79.       if (command->subshell && command->type != cm_group)
  80.     cprintf ("( ");
  81.  
  82.       switch (command->type)
  83.     {
  84.     case cm_for:
  85.       print_for_command (command->value.For);
  86.       break;
  87.  
  88.     case cm_case:
  89.       print_case_command (command->value.Case);
  90.       break;
  91.  
  92.     case cm_while:
  93.       print_while_command (command->value.While);
  94.       break;
  95.  
  96.     case cm_until:
  97.       print_until_command (command->value.While);
  98.       break;
  99.  
  100.     case cm_if:
  101.       print_if_command (command->value.If);
  102.       break;
  103.  
  104.     case cm_simple:
  105.       print_simple_command (command->value.Simple);
  106.       break;
  107.  
  108.     case cm_connection: 
  109.  
  110.       skip_this_indent++;
  111.       make_command_string_internal (command->value.Connection->first);
  112.  
  113.       switch (command->value.Connection->connector)
  114.         {
  115.         case '&':
  116.         case '|':
  117.           {
  118.         char c = command->value.Connection->connector;
  119.         cprintf ("%c", c);
  120.         if (c != '&' || command->value.Connection->second)
  121.           {
  122.             cprintf (" ");
  123.             skip_this_indent++;
  124.           }
  125.           }
  126.           break;
  127.  
  128.         case AND_AND:
  129.           cprintf (" && ");
  130.           if (command->value.Connection->second)
  131.         skip_this_indent++;
  132.           break;
  133.  
  134.         case OR_OR:
  135.           cprintf (" || ");
  136.           if (command->value.Connection->second)
  137.         skip_this_indent++;
  138.           break;
  139.     
  140.         case ';':
  141.         cprintf (";");
  142.  
  143.           if (inside_function_def)
  144.         cprintf ("\n");
  145.           else
  146.         {
  147.           cprintf (" ");
  148.           if (command->value.Connection->second)
  149.             skip_this_indent++;
  150.         }
  151.           break;
  152.  
  153.         default:
  154.           cprintf ("OOPS!  Bad connector `%d'!",
  155.                command->value.Connection->connector);
  156.           break;
  157.         }
  158.  
  159.       make_command_string_internal (command->value.Connection->second);
  160.       break;
  161.       
  162.     case cm_function_def:
  163.       print_function_def (command->value.Function_def);
  164.       break;
  165.  
  166.     case cm_group:
  167.       print_group_command (command->value.Group);
  168.       break;
  169.  
  170.     default:
  171.       programming_error ("OOPS!  Bad command type `%d'!", command->type);
  172.       break;
  173.     }
  174.  
  175.       if (command->subshell && command->type != cm_group)
  176.     cprintf (" )");
  177.  
  178.       if (command->redirects)
  179.     print_redirection_list (command->redirects);
  180.     }
  181. }
  182.  
  183. print_word_list (list, separator)
  184.      WORD_LIST *list;
  185.      char *separator;
  186. {
  187.   while (list)
  188.     {
  189.       printf ("%s", list->word->word);
  190.       list = list->next;
  191.       if (list)
  192.     printf ("%s", separator);
  193.     }
  194. }
  195.  
  196. command_print_word_list (list, separator)
  197.      WORD_LIST *list;
  198.      char *separator;
  199. {
  200.   while (list)
  201.     {
  202.       cprintf ("%s", list->word->word);
  203.       list = list->next;
  204.       if (list)
  205.     cprintf ("%s", separator);
  206.     }
  207. }
  208.  
  209. print_for_command (for_command)
  210.      FOR_COM *for_command;
  211. {
  212.   cprintf ("for %s in ", for_command->name->word);
  213.   command_print_word_list (for_command->map_list, " ");
  214.   newline ("do\n");
  215.   indentation += indentation_amount;
  216.   make_command_string_internal (for_command->action);
  217.   indentation -= indentation_amount;
  218.   newline ("done");
  219. }
  220.  
  221. print_group_command (group_command)
  222.      GROUP_COM *group_command;
  223. {
  224.   cprintf ("{ ");
  225.  
  226.   if (!inside_function_def)
  227.     skip_this_indent++;
  228.   else
  229.     cprintf ("\n");
  230.  
  231.   make_command_string_internal (group_command->command);
  232.  
  233.   if (inside_function_def)
  234.     cprintf (";\n}");
  235.   else
  236.     cprintf (" }");
  237. }
  238.  
  239. print_case_command (case_command)
  240.      CASE_COM *case_command;
  241. {
  242.   cprintf ("case %s in ", case_command->word->word);
  243.   if (case_command->clauses)
  244.     print_case_clauses (case_command->clauses);
  245.   newline ("esac");
  246. }
  247.  
  248. print_case_clauses (clauses)
  249.      PATTERN_LIST *clauses;
  250. {
  251.   indentation += (indentation_amount / 2);
  252.   while (clauses)
  253.     {
  254.       newline ("");
  255.       command_print_word_list (clauses->patterns, " | ");
  256.       cprintf (")\n");
  257.       indentation += (indentation_amount / 2);
  258.       make_command_string_internal (clauses->action);
  259.       indentation -= (indentation_amount / 2);
  260.       newline (";;");
  261.       clauses = clauses->next;
  262.     }
  263.   indentation -= (indentation_amount / 2);
  264. }
  265.  
  266. print_while_command (while_command)
  267.      WHILE_COM *while_command;
  268. {
  269.   print_until_or_while (while_command, "while");
  270. }
  271.  
  272. print_until_command (while_command)
  273.      WHILE_COM *while_command;
  274. {
  275.   print_until_or_while (while_command, "until");
  276. }
  277.  
  278. print_until_or_while (while_command, which)
  279.      WHILE_COM *while_command;
  280.      char *which;
  281. {
  282.   cprintf ("%s ", which);
  283.   skip_this_indent++;
  284.   make_command_string_internal (while_command->test);
  285.   newline ("do\n");
  286.   indentation += indentation_amount / 2;
  287.   make_command_string_internal (while_command->action);
  288.   indentation -= indentation_amount / 2;
  289.   newline ("done");
  290. }
  291.  
  292. print_if_command (if_command)
  293.      IF_COM *if_command;
  294. {
  295.   cprintf ("if ");
  296.   skip_this_indent++;
  297.   make_command_string_internal (if_command->test);
  298.   cprintf (" ; then\n");
  299.   indentation += indentation_amount / 2;
  300.   make_command_string_internal (if_command->true_case);
  301.   indentation -= indentation_amount / 2;
  302.  
  303.   if (if_command->false_case)
  304.     {
  305.       newline ("else\n");
  306.       indentation += indentation_amount / 2;
  307.       make_command_string_internal (if_command->false_case);
  308.       indentation -= indentation_amount / 2;
  309.     }
  310.   newline ("fi\n");
  311. }
  312.  
  313. print_simple_command (simple_command)
  314.      SIMPLE_COM *simple_command;
  315. {
  316.   command_print_word_list (simple_command->words, " ");
  317.   if (simple_command->redirects)
  318.     {
  319.       cprintf (" ");
  320.       print_redirection_list (simple_command->redirects);
  321.     }
  322. }
  323.  
  324. print_redirection_list (redirects)
  325.      REDIRECT *redirects;
  326. {
  327.   while (redirects)
  328.     {
  329.       print_redirection (redirects);
  330.       cprintf (" ");
  331.       redirects = redirects->next;
  332.     }
  333. }
  334.  
  335. print_redirection (redirect)
  336.      REDIRECT *redirect;
  337. {
  338.   int kill_leading = 0;
  339.   int redirector = redirect->redirector;
  340.   WORD_DESC *redirectee = redirect->redirectee.filename;
  341.  
  342.   switch (redirect->instruction)
  343.     {
  344.     case r_output_direction:
  345.       if (redirector != 1)
  346.     cprintf ("%d", redirector);
  347.       cprintf (">%s", redirectee->word);
  348.       break;
  349.  
  350.     case r_input_direction:
  351.       if (redirector != 0)
  352.     cprintf ("%d", redirector);
  353.       cprintf ("<%s", redirectee->word);
  354.       break;
  355.  
  356.     case r_inputa_direction:    /* Redirection created by the sh. */
  357.       cprintf ("&");
  358.       break;
  359.  
  360.     case r_appending_to:
  361.       if (redirector != 1)
  362.     cprintf ("%d", redirector);
  363.       cprintf (">>%s", redirectee->word);
  364.       break;
  365.  
  366.     case r_deblank_reading_until:
  367.       kill_leading++;
  368.       /* ... */
  369.     case r_reading_until:
  370.       if (redirector != 0)
  371.     cprintf ("%d", redirector);
  372.       cprintf ("<<%s%s", kill_leading? "-" : "", redirect->here_doc_eof);
  373.       break;
  374.  
  375.     case r_duplicating:
  376.       cprintf ("%d>&%d", redirector, (int)redirectee);
  377.       break;
  378.  
  379.     case r_close_this:
  380.       cprintf ("%d>&-", redirector);
  381.       break;
  382.  
  383.     case r_err_and_out:
  384.       cprintf (">&%s", redirectee->word);
  385.       break;
  386.     }
  387. }
  388.  
  389. static void
  390. reset_locals ()
  391. {
  392.   inside_function_def = 0;
  393.   indentation = 0;
  394. }
  395.  
  396. print_function_def (func)
  397.      FUNCTION_DEF *func;
  398. {
  399.   cprintf ("function %s () ", func->name->word);
  400.   add_unwind_protect (reset_locals, 0);
  401.   inside_function_def++;
  402.   skip_this_indent++;
  403.   indentation += indentation_amount;
  404.   make_command_string_internal (func->command);
  405.   remove_unwind_protect ();
  406.   indentation -= indentation_amount;
  407.   inside_function_def--;
  408. }
  409.  
  410. /* Return the string representation of the named function.
  411.    NAME is the name of the function.
  412.    COMMAND is the function body.  It should be a GROUP_COM.
  413.    MULTI_LINE is non-zero to pretty-print, or zero for all on one line.
  414.   */
  415. char *
  416. named_function_string (name, command, multi_line)
  417.      char *name;
  418.      COMMAND *command;
  419.      int multi_line;
  420. {
  421.   char *result;
  422.   int old_indent = indentation, old_amount = indentation_amount;
  423.   
  424.   command_string_index = 0;
  425.  
  426.   if (name && *name)
  427.     cprintf ("%s ", name);
  428.  
  429.   cprintf ("() \n");
  430.  
  431.   if (!multi_line)
  432.     {
  433.       indentation = 1;
  434.       indentation_amount = 0;
  435.     }
  436.   else
  437.     indentation += indentation_amount;
  438.  
  439.   inside_function_def++;
  440.   skip_this_indent++;
  441.   make_command_string_internal (command);
  442.  
  443.   indentation = old_indent;
  444.   indentation_amount = old_amount;
  445.   inside_function_def--;
  446.  
  447.   result = the_printed_command;
  448.  
  449.   if (!multi_line)
  450.     {
  451.       register int i;
  452.       for (i = 0; result[i]; i++)
  453.     if (result[i] == '\n')
  454.       {
  455.         strcpy (&result[i], &result[i + 1]);
  456.         --i;
  457.       }
  458.     }
  459.   return (result);
  460. }
  461.  
  462. newline (string)
  463.      char *string;
  464. {
  465.   cprintf ("\n");
  466.   indent (indentation);
  467.   cprintf ("%s", string);
  468. }
  469.  
  470. indent (amount)
  471.      int amount;
  472. {
  473.   while (amount-- > 0)
  474.     cprintf (" ");
  475. }
  476.  
  477. #if !defined (HAVE_VPRINTF)
  478. /* How to make the string. */
  479. cprintf (control, arg1, arg2, arg3, arg4, arg5)
  480.      char *control;
  481. {
  482.   char temp_buffer[5000];
  483.   int l;
  484.  
  485.   sprintf (temp_buffer, control, arg1, arg2, arg3, arg4, arg5);
  486.   l = strlen (temp_buffer);
  487.  
  488.   if (!the_printed_command)
  489.     the_printed_command =
  490.       (char *)xmalloc (the_printed_command_size = PRINTED_COMMAND_GROW_SIZE);
  491.  
  492.   while (the_printed_command_size <= command_string_index + l)
  493.     the_printed_command =
  494.       (char *)xrealloc (the_printed_command,
  495.             the_printed_command_size += PRINTED_COMMAND_GROW_SIZE);
  496.  
  497.   strcpy (the_printed_command + command_string_index, temp_buffer);
  498.   command_string_index += l;
  499. }
  500.  
  501. #else /* We have support for varargs. */
  502.  
  503. /* How to make the string. */
  504. cprintf (va_alist)
  505.      va_dcl
  506. {
  507.   char temp_buffer[5000];
  508.   int l;
  509.   char *control;
  510.   va_list args;
  511.  
  512.   va_start (args);
  513.   control = va_arg (args, char *);
  514.   vsprintf (temp_buffer, control, args);
  515.   va_end (args);
  516.   l = strlen (temp_buffer);
  517.  
  518.   if (!the_printed_command)
  519.     the_printed_command =
  520.       (char *)xmalloc (the_printed_command_size = PRINTED_COMMAND_GROW_SIZE);
  521.  
  522.   while (the_printed_command_size <= command_string_index + l)
  523.     the_printed_command =
  524.       (char *)xrealloc (the_printed_command,
  525.             the_printed_command_size += PRINTED_COMMAND_GROW_SIZE);
  526.  
  527.   strcpy (the_printed_command + command_string_index, temp_buffer);
  528.   command_string_index += l;
  529. }
  530. #endif /* HAVE_VPRINTF */
  531.